home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / sign.r < prev    next >
Text File  |  1994-04-25  |  654b  |  34 lines

  1. //-------------------------------------------------------------------//
  2. //
  3. // 1st cut at sign function
  4. // Return 1,  if element >= 0
  5. // Return 0,  if element == 0
  6. // Return -1, if element < 0
  7. //
  8. // Does not handle complex quantities like MATLAB
  9. //
  10.  
  11. sign = function ( a )
  12. {
  13.   local(i, j, r);
  14.   
  15.   if (class (a) != "num") { error ("sign() requires NUMERIC arg"); }
  16.   if (type (a) == "complex") 
  17.     {
  18.       return a ./ abs(a);
  19.     }
  20.  
  21.   r = zeros( size (a) );
  22.   for(i in 1:size (a)[1]) 
  23.     {
  24.       for(j in 1:size (a)[2]) 
  25.     {
  26.       if(a[i;j] >  0) { r[i;j] = 1; }
  27.       if(a[i;j] == 0) { r[i;j] = 0; }
  28.       if(a[i;j] <  0) { r[i;j] =-1; }
  29.     }
  30.     }
  31.  
  32.   return r;
  33. };
  34.